package com.sf.osms.dep.processor.downloader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sf.osms.dep.domain.Agent;
import com.sf.osms.dep.domain.AgentFileMap;
import com.sf.osms.dep.processor.util.CollectionUtil;
import com.sf.osms.dep.processor.util.ExceptionUtils;
import com.sf.osms.dep.processor.util.FTPUtil;

public class FTPEventDownloader implements EventDownloader{
	
	private static Logger logger = LoggerFactory.getLogger(FTPEventDownloader.class);
	@Override
	public List<File> downloadFiles(Agent agentInfo) throws Exception {
		
		if(null == agentInfo){
			return null;
		}
		
		String remotePath = agentInfo.getUploadPath();
		String remoteBackupPath = agentInfo.getBackPath();
		AgentFileMap parseRules = agentInfo.getAgentFileMap();
		
		String filePrefix = parseRules.getFilePrefix();
		String fileSuffix = parseRules.getFileSuffix();
		String agentCode = agentInfo.getName();
		List<String> fileStrs = null;
		List<File> downloadFiles = new ArrayList<File>();
		try {
			fileStrs = FTPUtil.downloadTrackFiles(remotePath, remoteBackupPath, filePrefix, fileSuffix,agentCode);
		} catch (Exception e) {
			logger.error("******FTP下载出现异常:"+ExceptionUtils.getExceptionStackString(e)+"******");
			throw e;
		}
		if(!CollectionUtil.isEmpty(fileStrs)){
			
			for (String fileStr : fileStrs) {
				downloadFiles.add(new File(fileStr));
			}
		}
		
		
		return downloadFiles;
	}

}
